const express = require('express')
const Mock = require('mockjs')
const router = express.Router()

const departments = ['内科', '外科', '五官科', '儿科', '消化内科', '骨科', '康复科']
const titles = ['住院医师', '主治医师', '副主任医师', '主任医师']

// GET，获取预约挂号列表数据
router.get('/:id?', (req, res) => {
  const { id } = req.params
  if (id) {
    const data = Mock.mock({
      'id|+1': 1,
      cardNumber: '@string("number", 12)',
      idCard: '@string("number", 18)',
      phone: '@string("number", 11)',
      patientName: '@cname',
      address: '@county(true)',
      'departmentId|1-7': 1,
      'departmentName': departments[Math.floor(Math.random() * departments.length)],
      'doctorId|1-10': 1,
      'doctorName': '@cname',
      'title|1': titles,
      'fee|5-15': 5,
      'date|1': ['@date("yyyy-MM-dd") 上午', '@date("yyyy-MM-dd") 下午'],
      createdAt: '@integer(1579234123414, 1679234123414)',
      'status|1': [0, 1, 2], // 0: 已取消 1. 未就诊 2. 已就诊
    })
    res.json({
      code: 200,
      data: {
        detail: data
      }
    })
    return
  }

  const { page = 1, pageSize = 5 } = req.query
  // mock 随机数据
  const data = Mock.mock({
    [`list|${pageSize}`]: [
      {
        'id|+1': 1,
        name: '@cname',
        cardNumber: '@string("number", 12)',
        idCard: '@string("number", 18)',
        phone: '@string("number", 11)',
        'department|1': departments,
        'fee|5-15.2-2': 5.55,
        date: '@integer(1679234123414,1979234123414)',
        createdAt: '@integer(1579234123414, 1679234123414)',
        'status|1': [0, 1, 2], // 0: 已取消 1. 未就诊 2. 已就诊
      }
    ],
    page: Number(page),
    pageSize: Number(pageSize),
    total: 372
  })

  res.json({
    code: 200,
    data
  })
})

// 挂号
router.post('/', (req, res) => {
  const { address, cardNumber, date, departmentName, doctorName, idCard, patientName, fee, phone, title, } = req.body
  const data = {
    id: 999,
    address,
    cardNumber,
    date,
    departmentName,
    doctorName,
    idCard,
    patientName,
    phone,
    title,
    fee,
    createdAt: Date.now(),
    status: 1,
  }

  res.json({
    code: 200,
    data: {
      detail: data
    }
  })
})

// 修改
router.put('/', (req, res) => {
  const { id, address, cardNumber, date, departmentName, doctorName, idCard, createdAt, patientName, fee, phone, title, status } = req.body
  const data = ({
    id,
    address,
    cardNumber,
    date,
    departmentName,
    doctorName,
    idCard,
    patientName,
    phone,
    title,
    fee,
    createdAt,
    status,
  })

  res.json({
    code: 200,
    data: {
      detail: data
    }
  })
})

router.delete('/:id', (req, res) => {
  res.json({
    code: 200,
    data: {
      message: '删除成功'
    }
  })
})

module.exports = router
